package com.ydcode.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

public class PropertiesUtil {

	private static Logger logger = Logger.getLogger(PropertiesUtil.class);

	/**
	 * 保存配置文件
	 * 
	 * @param map
	 *            键值对
	 * @param pathname
	 *            文件保存路径
	 * @param comments
	 *            描述
	 * @return 生成后的Properties
	 */
	public static Properties savePropertiesFile(Map<String, Object> map,
			String pathname, String comments) {

		Properties properties = new Properties();
		OutputStream out = null;
		File file = new File(pathname);
		if (file.exists()) {
			file.delete();
			logger.debug(file.getPath() + "delete");
		}
		try {
			out = new FileOutputStream(pathname, true);
			for (String key : map.keySet()) {
				if (key == null || key.equals("")) {
					continue;
				}
				Object value = map.get(key);
				if (value == null) {
					continue;
				}
				properties.setProperty(key, value.toString());
			}
			properties.store(out, comments);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error("", e.getCause());
		} catch (IOException e) {
			logger.error("", e.getCause());
		} finally {
			if (out != null) {
				try {
					out.flush();
					out.close();
				} catch (IOException e) {
					logger.error("IOException", e.getCause());
				}
				out = null;
			}
		}
		return properties;
	}

	/**
	 * 加载配置文件
	 * 
	 * @param pathname
	 *            文件地址名称
	 * @return 配置信息
	 */
	public static Properties loadPropertiesFile(String pathname) {
		Properties properties = new Properties();
		File file = new File(pathname);
		if (file.exists()) {
			logger.error("文件不存在");
		}
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(file);
			properties.load(inputStream);
		} catch (FileNotFoundException e) {
			logger.error("FileNotFoundException", e.getCause());
		} catch (IOException e) {
			logger.error("IOException", e.getCause());
		} finally {
			try {
				inputStream.close();
			} catch (IOException e) {
				logger.error("inputStream.close() IOException", e.getCause());
			}
		}
		return properties;
	}

	/**
	 * 加载配置文件
	 * 
	 * @param pathname
	 *            文件路径名称
	 * @return HashMap格式的配置信息
	 */
	@SuppressWarnings("rawtypes")
	public static HashMap<String, String> loadMapFile(String pathname) {

		HashMap<String, String> hashMap = new HashMap<String, String>();
		Properties properties = loadPropertiesFile(pathname);

		Enumeration en = properties.propertyNames();
		while (en.hasMoreElements()) {
			String key = (String) en.nextElement();
			String property = properties.getProperty(key);
			hashMap.put(key, property);
		}

		return hashMap;
	}
}
